<?php    
session_start();
 if (empty($_SESSION['username']) AND empty($_SESSION['passuser'])){
  echo "<link href='style.css' rel='stylesheet' type='text/css'>
 <center>Untuk mengakses modul, Anda harus login <br>";
  echo "<a href=../../index.php><b>LOGIN</b></a></center>";
}
else{

function GetCheckboxes($table, $key, $Label, $Nilai='') {
  $s = "select * from $table order by nama_tag";
  $r = mysql_query($s);
  $_arrNilai = explode(',', $Nilai);
  $str = '';
  while ($w = mysql_fetch_array($r)) {
    $_ck = (array_search($w[$key], $_arrNilai) === false)? '' : 'checked';
    $str .= "<input type=checkbox name='".$key."[]' value='$w[$key]' $_ck>$w[$Label] ";
  }
  return $str;
}

$aksi="modul/mod_siswa/aksi_siswa.php";
switch($_GET[act]){
  // Tampil siswa
  default:
    echo "<h2>Data Siswa</h2>
          <form method=get action='$_SERVER[PHP_SELF]'>
          <input type=hidden name=module value=siswa>
          <div id=paging>Nama siswa : <input type=text name='kata'> <input type=submit value=Cari></div>
          </form>
          <input type=button value='Tambah Siswa' onclick=\"window.location.href='?module=siswa&act=tambahsiswa';\">";

    if (empty($_GET['kata'])){
    echo "<table>  
          <tr><th>no</th><th>nama</th><th>tgl. posting</th><th>aksi</th></tr>";

    $p      = new Paging;
    $batas  = 15;
    $posisi = $p->cariPosisi($batas);

    if ($_SESSION[leveluser]=='admin'){
      $tampil = mysql_query("SELECT * FROM siswa ORDER BY id_siswa ASC LIMIT $posisi,$batas");
    }
    else{
      $tampil=mysql_query("SELECT * FROM siswa 
                           WHERE username='$_SESSION[namauser]'       
                           ORDER BY id_siswa ASC LIMIT $posisi,$batas");
    }
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tgl_posting=tgl_indo($r[tanggal]);
      echo "<tr><td>$no</td>
                <td>$r[nama]</td>
                <td>$tgl_posting</td>
		            <td><a href=?module=siswa&act=editsiswa&id=$r[id_siswa]>Edit</a> | 
		                <a href='$aksi?module=siswa&act=hapus&id=$r[id_siswa]&namafile=$r[gambar]' onClick=\"return confirm('Anda yakin akan menghapus siswa?')\">Hapus</a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    if ($_SESSION[leveluser]=='admin'){
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM siswa"));
    }
    else{
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM siswa WHERE username='$_SESSION[namauser]'"));
    }  
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET[halaman], $jmlhalaman);

    echo "<div id=paging>$linkHalaman</div><br>";
 
    break;    
    }
    else{
    echo "<table>  
          <tr><th>no</th><th>nama</th><th>tgl. posting</th><th>aksi</th></tr>";

    $p      = new Paging9;
    $batas  = 15;
    $posisi = $p->cariPosisi($batas);

    if ($_SESSION[leveluser]=='admin'){
      $tampil = mysql_query("SELECT * FROM siswa WHERE nama LIKE '%$_GET[kata]%' ORDER BY id_siswa ASC LIMIT $posisi,$batas");
    }
    else{
      $tampil=mysql_query("SELECT * FROM siswa 
                           WHERE username='$_SESSION[namauser]'
                           AND nama LIKE '%$_GET[kata]%'       
                           ORDER BY id_siswa ASC LIMIT $posisi,$batas");
    }
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tgl_posting=tgl_indo($r[tanggal]);
      echo "<tr><td>$no</td>
                <td>$r[nama]</td>
                <td>$tgl_posting</td>
		            <td><a href=?module=siswa&act=editsiswa&id=$r[id_siswa]>Edit</a> | 
		                <a href='$aksi?module=siswa&act=hapus&id=$r[id_siswa]&namafile=$r[gambar]'>Hapus</a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    if ($_SESSION[leveluser]=='admin'){
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM siswa WHERE nama LIKE '%$_GET[kata]%'"));
    }
    else{
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM siswa WHERE username='$_SESSION[namauser]' AND nama LIKE '%$_GET[kata]%'"));
    }  
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET[halaman], $jmlhalaman);

    echo "<div id=paging>$linkHalaman</div><br>";
 
    break;    
    }

  
  case "tambahsiswa":
    echo "<h2>Tambah Siswa</h2>
          <form method=POST action='$aksi?module=siswa&act=input' enctype='multipart/form-data'>
          <table>
          <tr><td width=70>Nama</td>     <td> : <input type=text name='nama' size=60></td></tr>
          <tr><td>Kelas</td>  <td> : 
          <select name='kelas'>
            <option value=0 selected>- Pilih Kelas -</option>";
            $tampil=mysql_query("SELECT * FROM kelas ORDER BY nama_kelas");
            while($r=mysql_fetch_array($tampil)){
              echo "<option value=$r[id_kelas]>$r[nama_kelas]</option>";
            }
    echo "</select></td></tr>
           
          <tr><td>Deskripsi</td>  <td> <textarea name='alamat'  style='width: 400px; height: 50px;'></textarea></td></tr>
          <tr><td>Gambar</td>      <td> : <input type=file name='fupload' size=40> 
                                          <br>Tipe gambar harus JPG/JPEG dan ukuran lebar maks: 400 px</td></tr>";

    
    
    echo "</td></tr>
          <tr><td colspan=2><input type=submit value=Simpan>
                            <input type=button value=Batal onclick=self.history.back()></td></tr>
          </table></form>";
     break;
    
    
  case "editsiswa":
    $edit = mysql_query("SELECT * FROM siswa WHERE id_siswa='$_GET[id]' AND username='$_SESSION[namauser]'");
    $r    = mysql_fetch_array($edit);

    echo "<h2>Edit siswa</h2>
          <form method=POST enctype='multipart/form-data' action=$aksi?module=siswa&act=update>
          <input type=hidden name=id value=$r[id_siswa]>
          <table>
          <tr><td width=70>Nama</td>     <td> : <input type=text name='nama' size=60 value='$r[nama]'></td></tr>
          <tr><td>Kelas</td>  <td> : <select name='kelas'>";
 
          $tampil=mysql_query("SELECT * FROM kelas ORDER BY nama_kelas");
          if ($r[id_kelas]==0){
            echo "<option value=0 selected>- Pilih Kelas -</option>";
          }   

          while($w=mysql_fetch_array($tampil)){
            if ($r[id_kelas]==$w[id_kelas]){
              echo "<option value=$w[id_kelas] selected>$w[nama_kelas]</option>";
            }
            else{
              echo "<option value=$w[id_kelas]>$w[nama_kelas]</option>";
            }
          }

    echo "</select></td></tr>";

   if ($r[headline]=='Y'){
      echo "<tr><td>Headline</td> <td> : <input type=radio name='headline' value='Y' checked>Y  
                                        <input type=radio name='headline' value='N'> N</td></tr>";
    }
    else{
      echo "<tr><td>Headline</td> <td> : <input type=radio name='headline' value='Y'>Y  
                                        <input type=radio name='headline' value='N' checked>N</td></tr>";
    }
      echo "<tr><td>deskripsi</td>   <td> <textarea name='alamat' style='width: 400px; height: 50px;'>$r[alamat]</textarea></td></tr>
          <tr><td>Gambar</td>       <td> :  ";
          if ($r[gambar]!=''){
              echo "<img src='../foto_siswa/small_$r[gambar]'>";  
          }
    echo "</td></tr>
          <tr><td>Ganti Gbr</td>    <td> : <input type=file name='fupload' size=30> *)</td></tr>
          <tr><td colspan=2>*) Apabila gambar tidak diubah, dikosongkan saja.</td></tr>";

    
 
    echo  "<tr><td colspan=2><input type=submit value=Update>
                            <input type=button value=Batal onclick=self.history.back()></td></tr>
         </table></form>";
    break;  
}

}
?>
